package com.jwthhealth_pub.wristband.sleep.view;

import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.idtk.smallchart.data.LineData;
import com.idtk.smallchart.interfaces.iData.ILineData;
import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.DataUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.utils.TimeUtil;
import com.jwthhealth_pub.wristband.sleep.widget.SuperCircleView;

import java.text.NumberFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 睡眠统计
 * Created by yang on 2017/12/15.
 */

public class SleepStatisticsActivity extends BaseActivity {

    private final String TAG = makeLogTag(SleepStatisticsActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;

    @BindView(R.id.superview)
    SuperCircleView mSuperCircleView;

    @BindView(R.id.superviewRight)
    SuperCircleView mSuperCircleViewRight;
    @BindView(R.id.tv)
    TextView tv_left_sleep;
    @BindView(R.id.tv_right)
    TextView tv_right_sleep;
    @BindView(R.id.tv_Sleepearly)
    TextView tv_Sleepearly;    //睡的早
    @BindView(R.id.tv_Sleeplong)
    TextView tv_Sleeplong;  //睡的长
    @BindView(R.id.tv_deepSleep)
    TextView tv_deepSleep;    //深睡
    @BindView(R.id.tv_lightSleep)
    TextView tv_lightSleep;  //浅睡
    @BindView(R.id.tv_sober)
    TextView tv_sober;    //清醒
    @BindView(R.id.tv_startSleep)
    TextView tv_startSleep;  //开始睡眠
    @BindView(R.id.tv_endSleep)
    TextView tv_endSleep;  //结束睡眠
    @BindView(R.id.tv_title)
    TextView tv_title;  //显示日期


    @BindView(R.id.chart1)
    LineChart mChart;
    private LineData mLineData = new LineData();
    private ArrayList<ILineData> mDataList = new ArrayList<>();
    private ArrayList<PointF> mPointArrayList = new ArrayList<>();
    protected int[] mColors = {R.color.primarycolor};

    ArrayList<String> sleepValues = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sleep_statistics);
        ButterKnife.bind(this);
        sleepValues =(ArrayList<String>)getIntent().getStringArrayListExtra("sleepValues");
        initData();
        initCircleView();
        intChart();
    }

    private void initData() {
        tv_title.setText(TimeUtil.getPastDate(1));
        if(sleepValues.size()>0){
            tv_deepSleep.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValues.get(2))));
            tv_lightSleep.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValues.get(3))));
            tv_sober.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValues.get(4))));
            tv_startSleep.setText(DataUtil.timeStamp2Date(sleepValues.get(0), null));
            tv_endSleep.setText(DataUtil.timeStamp2Date(sleepValues.get(1), null));
        }

    }

    //这是顶部折线统计
    private void intChart() {

        mChart.getDescription().setEnabled(false);
        mChart.setTouchEnabled(false);
        mChart.setDragEnabled(false);
        mChart.setScaleEnabled(true);
        mChart.setDrawGridBackground(false);   //设置背景是否网格显示
        mChart.setPinchZoom(true);
        mChart.setDrawingCacheEnabled(false);
        Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setAxisMaximum(40);  //设置y的最大值
        leftAxis.setAxisMinimum(0);  //设置y的最小值
        IAxisValueFormatter custom =  new SValueFormatter();  //这是y的自定义单位
        leftAxis.setValueFormatter(custom);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        leftAxis.setDrawZeroLine(false);

        leftAxis.setDrawLimitLinesBehindData(false);

        mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        IAxisValueFormatter xAxisFormatter = new DayFormatter();
        mChart.getXAxis().setValueFormatter(xAxisFormatter);
        mChart.getAxisRight().setEnabled(false);

        setData();  //设置数据源

        mChart.animateX(2500);  //动画
        mChart.invalidate();
        Legend l = mChart.getLegend();     //底部显示数据
        l.setForm(Legend.LegendForm.LINE);
        l.setEnabled(false);
    }
    private void setData() {
        ArrayList<Entry> values = new ArrayList<Entry>();
        if(sleepValues.size()>0){

            for (int i=0; i<24; i++) {
                Log.i("tag",Integer.parseInt(sleepValues.get(i+5))+"ccccccc");
                if(Integer.parseInt(sleepValues.get(i+5))>=0&&Integer.parseInt(sleepValues.get(i+5))<6){
                    values.add(new Entry(i,10 ));
                }else if(Integer.parseInt(sleepValues.get(i+5))>=6&&Integer.parseInt(sleepValues.get(i+5))<15){
                    values.add(new Entry(i,20 ));
                }else if(Integer.parseInt(sleepValues.get(i+5))>=15){
                    values.add(new Entry(i,30 ));
                }
            }
        }


        LineDataSet set1;
        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set1 = new LineDataSet(values, "DataSet 1");
            set1.setDrawIcons(false);
            set1.setColor(getResources().getColor(R.color.primarycolor));
            set1.setCircleColor(Color.TRANSPARENT);
            set1.setLineWidth(3f);
            set1.setDrawCircleHole(true);
            set1.setValueTextColor(Color.TRANSPARENT);
            set1.setValueTextSize(9f);
            set1.setFormLineWidth(1f);
            set1.setFormSize(15.f);

            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            dataSets.add(set1); // add the datasets

            com.github.mikephil.charting.data.LineData data = new com.github.mikephil.charting.data.LineData(dataSets);

            mChart.setData(data);
        }
    }

    //这是底部圆形统计
    private void initCircleView() {
        //left
        mSuperCircleView.setShowSelect(false);
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
        valueAnimator.setTarget(tv_left_sleep);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int i = Integer.valueOf(String.valueOf(animation.getAnimatedValue()));
                int value = 120;
                mSuperCircleView.setSelect((int) (value * (i / 100f)));
                // 创建一个数值格式化对象
                NumberFormat numberFormat = NumberFormat.getInstance();
                // 设置精确到小数点后几位
                numberFormat.setMaximumFractionDigits(0);
                String result = numberFormat.format((float) value / (float) 360 * 100);
                tv_left_sleep.setTextColor(getResources().getColor(R.color.primarycolor));
                tv_left_sleep.setText( result+"%");
            }
        });
        valueAnimator.start();
        //right
        mSuperCircleViewRight.setShowSelect(false);
        ValueAnimator valueAnimatorRight = ValueAnimator.ofInt(0, 100);
        valueAnimatorRight.setTarget(tv_right_sleep);
        valueAnimatorRight.setDuration(2000);
        valueAnimatorRight.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int i = Integer.valueOf(String.valueOf(animation.getAnimatedValue()));
                int value = 230;
                int [] colorCilreArray = {R.color.sleepRightColor,R.color.sleepRightColor,R.color.sleepRightColor};
                mSuperCircleViewRight.setColor(colorCilreArray);
                mSuperCircleViewRight.setSelect((int) (value * (i / 100f)));
                // 创建一个数值格式化对象
                NumberFormat numberFormat = NumberFormat.getInstance();
                // 设置精确到小数点后几位
                numberFormat.setMaximumFractionDigits(0);
                String result = numberFormat.format((float) value / (float) 360 * 100);
                tv_right_sleep.setTextColor(getResources().getColor(R.color.sleepRightColor));
                tv_right_sleep.setText( result+"%");
            }
        });
        valueAnimatorRight.start();
    }
    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }
    //这是设置下端的数值
    class DayFormatter implements IAxisValueFormatter
    {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {

            String appendix="";
            if(value==0.0){
                appendix = "00:00";
            }else if(value==4.0){
                appendix = "04:00";
            }else  if(value==8.0){
                appendix = "08:00";
            }else if(value==12.0){
                appendix = "12:00";
            }else if(value==16.0){
                appendix = "16:00";
            }else if(value==20.0){
                appendix = "20:00";
            }else if(value==24.0){
                appendix = "24:00";
            }else{
                appendix = "";
            }
            return appendix;
        }

    }

     class SValueFormatter implements IAxisValueFormatter
    {


        private String appendix;


        @Override
        public String getFormattedValue(float value, AxisBase axis) {
              if(value==10.0){
                  appendix = "深睡";
              }else if(value==20.0){
                  appendix = "浅睡";
              }else if(value==30.0){
                  appendix = "清醒";
              }else{
                  appendix = "";
              }
            return appendix;
        }


    }




}
